﻿using System;
using System.Collections.Generic;
using System.Text;
using PPI.Entities.Distribution;
using PPI.Entities.Inspections;
using PPI.Mobile.Logic;
using System.Data;
using VMind.Global;

namespace PPI.Client.Logic.Distribution
{
    public class LInspectionResult
    {
        public static List<EInspectionTypes> SelectAllIdName()
        {
            DataTable dt = WebConsumer.Distribution.WDistributionInspectionType.SelectAllIdName().Tables[0];
            List<EInspectionTypes> lstInspectionResult = new List<EInspectionTypes>();

            foreach (DataRow drTreatment in dt.Rows)
            {
                EInspectionTypes inspectionResult = new EInspectionTypes();

                inspectionResult.Id = ConvertHelper.ToInt32(drTreatment["Vendor_InspVisit_XmissionStructures_Inspection_TypeId"]);
                inspectionResult.Name = drTreatment["Name"].ToString();

                lstInspectionResult.Add(inspectionResult);
            }

            return lstInspectionResult;
        }

        public static Int32 Save(EDistributionInspectionResult inspectionResult, Int32 poleId)
        {
            return WebConsumer.Distribution.WPole.Save(inspectionResult, poleId);
        }

        public static EDistributionInspectionResult GetPoleInspectionResults(EDistributionPole distributionPole, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   PIR.PoleInspectionResultId, ");
            _sql.AppendLine("   PIR.PoleInspectionResultServerID, ");
            _sql.AppendLine("   PIR.InspectionTypeFk, ");
            _sql.AppendLine("   PIR.Comments, ");
            _sql.AppendLine("   PIR.isElectrical, ");
            _sql.AppendLine("   PIR.ResultTypeFk, ");
            _sql.AppendLine("   LKI.LookupText InspectionName, ");
            _sql.AppendLine("   LKR.LookupText ResultName ");
            _sql.AppendLine("FROM tb_Pole_InspectionResult PIR ");
            _sql.AppendLine("LEFT JOIN tb_Lookups LKR ON LKR.LookupValue = PIR.ResultTypeFk AND LKR.LookupTypeFk = 'ResultType' ");
            _sql.AppendLine("LEFT JOIN tb_Lookups LKI ON LKI.LookupValue = PIR.InspectionTypeFk AND LKI.LookupTypeFk = 'InspectionType' ");
            _sql.AppendLine("WHERE PIR.PoleFk = " + distributionPole.Id + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];

            EDistributionInspectionResult inspResult = new EDistributionInspectionResult();

            foreach (DataRow dr in dt.Rows)
            {
                inspResult.Id = ConvertHelper.ToInt32(dr["PoleInspectionResultId"]);
                inspResult.ServerId = ConvertHelper.ToInt32(dr["PoleInspectionResultServerID"]);
                inspResult.InspectionType.Id = ConvertHelper.ToInt32(dr["InspectionTypeFk"]);
                inspResult.InspectionType.Name = dr["InspectionName"].ToString();
                inspResult.Comments = dr["Comments"].ToString();
                inspResult.IsElectrical = ConvertHelper.Tobool(dr["isElectrical"]);
                inspResult.Resultype.Id = ConvertHelper.ToInt32(dr["ResultTypeFk"]);
                inspResult.Resultype.Name = dr["ResultName"].ToString();
            }

            return inspResult;
        }

        public static EDistributionInspectionResult GetProjectPoleInspectionResults(EDistributionPole distributionPole, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   PIR.PoleInspectionResultId, ");
            _sql.AppendLine("   PIR.InspectionTypeFk, ");
            _sql.AppendLine("   PIR.Comments, ");
            _sql.AppendLine("   PIR.isElectrical, ");
            _sql.AppendLine("   PIR.ResultTypeFk, ");
            _sql.AppendLine("   LKI.LookupText InspectionName, ");
            _sql.AppendLine("   LKR.LookupText ResultName ");
            _sql.AppendLine("FROM tb_Pole_InspectionResult PIR ");
            _sql.AppendLine("LEFT JOIN tb_Lookups LKI ON LKI.LookupValue = PIR.InspectionTypeFk AND LKI.LookupTypeFk = 'InspectionType' ");
            _sql.AppendLine("LEFT JOIN tb_Lookups LKR ON LKR.LookupValue = PIR.ResultTypeFk AND LKR.LookupTypeFk = 'ResultType' ");
            _sql.AppendLine("WHERE PIR.PoleFk = " + distributionPole.Id + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];

            EDistributionInspectionResult inspResult = new EDistributionInspectionResult();

            foreach (DataRow dr in dt.Rows)
            {
                inspResult.Id = ConvertHelper.ToInt32(dr["PoleInspectionResultId"]);
                inspResult.InspectionType.Id = ConvertHelper.ToInt32(dr["InspectionTypeFk"]);
                inspResult.InspectionType.Name = dr["InspectionName"].ToString();
                inspResult.Comments = dr["Comments"].ToString();
                inspResult.IsElectrical = ConvertHelper.Tobool(dr["isElectrical"]);
                inspResult.Resultype.Id = ConvertHelper.ToInt32(dr["ResultTypeFk"]);
                inspResult.Resultype.Name = dr["ResultName"].ToString();
            }

            return inspResult;
        }
    }
}
